﻿//条目动作类型
packageContent.itemActionType = function(partObjId,actionItemId,partObjCD) {
	var _actionItemId = actionItemId;
	var _partObjId = partObjId;
	var _partObjCD = partObjCD;//对象number
	var _initActionType = function() {
		//基本信息赋值
		if ((pack.getActionItemMap() == undefined)
				|| (pack.getActionItemMap().get(_actionItemId) == null)) {
			return;
		}

		//初始化动作类型
		var actionTypeMap = ACTION.TYPEMAP();
		var keys = actionTypeMap.keys();
		var _action;
		var _actionName;
		var option;
		for (var i =0; i < keys.length; i++) {
			_action = keys[i];
			_actionName = actionTypeMap.get(keys[i]);
			//参与对象设置[客户]时，动作只能是【订购】
			if ((_partObjId == OBJECT.PARTY) && (_action == ACTION.TYPE.D)) {
		
				option = $("<option>");
				option.attr("value", _action);
				option.html(_actionName);
				$("#p_action_type").append(option);
				
				break;
			} 
			option = $("<option>");
			option.attr("value", _action);
			option.html(_actionName);
			$("#p_action_type").append(option);
		}
		
		var actionItem = pack.getActionItemMap().get(actionItemId);

		$("#p_action_type").val(actionItem.getActionTypeCd());
		
		if (actionItem.getActionTypeCd()) {
			$("#p_action_type").attr("disabled",true);
		}
	};
	var _initButton = function() {
		$("#p_actionType_save").bind("click", function() {
			var _flag = _saveActionType();
			if (_flag) {
				_doClose();
			}
		});
		$("#p_actionType_cancel").bind("click", function() {
			_doClose();
		});
		
		$("#p_actionType_save").addClass("btn-blue");
		$("#p_actionType_cancel").addClass("btn-gray");
	}

	var _validate = function() {
		if (Validate.isNull($("#p_action_type").val())) {
			PACK.ALERT.warn("请选择动作！");
			return false;
		}
		if (ACTION.TYPE.NEW == $("#p_action_type").val()) {
				if (Validate.isNull(_partObjId)) {
					alert("【新装】时，请首先设定参与对象，谢谢!");
					return ;
				}
			}
		return true;
	}
	
	//保存
	var _saveActionType = function() {
		//校验
		var _flag = _validate();
		if (!_flag) {
			return _flag;
		}
		var actionType = $("#p_action_type").val();
		//重新赋值
		var _packActionItem;
		if (!(pack.getActionItemMap() == undefined)
				&& !(pack.getActionItemMap().get(actionItemId) == null)) {
			_packActionItem = pack.getActionItemMap().get(actionItemId);
		} else {
			_packActionItem = new PackageActionItem();
		}

		_packActionItem.setActionItemId(_actionItemId);
		_packActionItem.setActionTypeCd(actionType);
		

		//【新装】动作，构造完整条目信息
		if (ACTION.TYPE.NEW == actionType) {
			_packActionItem.setEffectobjNumber(_partObjCD);
			_packActionItem.setPartobjNumber(_partObjCD);
			
			var partObj = pack.getPackObjMap().get(_partObjCD);
			var objName = partObj.getObjectName();
			
			//整体逻辑 ：第二列 第2个
			var itemActionTd = $("#" + _actionItemId).next().find("td")[1];
			$(itemActionTd).find("span").attr("title",objName);
			if(objName.length > 18) {
				objName = objName.substring(0,18)+"...";
			}
			$(itemActionTd).find("span").html(objName);
			$(itemActionTd).find("span").attr("effectObjID",_partObjCD);
			$(itemActionTd).find("span").parents('td').removeAttr('style');
			$(itemActionTd).find("span").parents('td').unbind('click');
		}
		
		var actionItemMap;
		if (pack.getActionItemMap() == undefined) {
			actionItemMap = new HashMap();
		} else {
			actionItemMap = pack.getActionItemMap();
		}
		actionItemMap.put(_actionItemId, _packActionItem);

		pack.setActionItemMap(actionItemMap);

		var selectText = $("#p_action_type option:selected").text();
		//整体逻辑 ：第二列 第一个
		var itemActionTd = $("#" + _actionItemId).next().find("td")[0];
		$(itemActionTd).find("span").html(selectText);
		$(itemActionTd).find("span").attr("actionType",actionType);
		
		return _flag;
	}

	//关闭
	var _doClose = function() {
		$("#openActionTypePage").window('close');
	}

	//初始化包参与对象
	var _init = function() {
		_initActionType();
		_initButton();
	}

	_init();
}

